<?php

namespace paper\tpl;

use paper\exception\DbException;

/** @var integer $line */
/** @var integer $start */
/** @var string $file */
/** @var array $nl */
/** @var array $trace */

/** @var string $subMessage */
class ExceptionTpl implements Tpl
{
    private string $message;
    private ?string $subMessage = null;
    private string $file;
    private int $line;
    private array $trace;

    public function __construct(\Throwable $throwable)
    {
        $this->message = $throwable->getMessage();
        $this->file = $throwable->getFile();
        $this->line = $throwable->getLine();
        $this->trace = $throwable->getTrace();
        if ($throwable instanceof DbException) {
            $this->subMessage = $throwable->getSql();
            $bindData = $throwable->getBindData();
            $this->subMessage .= "<br>[";
            foreach ($bindData as $key => $datum) {
                $this->subMessage .= " $key=>$datum";
            }
            $this->subMessage .= ']';
        }
    }

    function run()
    {
        if (true) {
            if (is_file($this->file))
                $fileContent = htmlspecialchars(file_get_contents($this->file), ENT_QUOTES);
            else
                $fileContent = "";
            $fileContent = str_replace("\r\n", "\n", $fileContent);
            $fileContent = str_replace(" ", "&nbsp;", $fileContent);
            $lines = explode("\n", $fileContent);
            if (count($lines) <= 20) {
                $start = 0;
            } else {
                $start = $this->line - 10;
                $start = $start < 0 ? 0 : $start;
            }
            $nl = array_slice($lines, $start, 20);
            //  include(CORE_PATH . '/tpl/exception.php');
        }
        ?>
        <html lang="zh">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <style>
            html, body {
                font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
                font-size: 16px;
                margin: 0;
                padding: 10px;
            }

            .source {
                padding: 10px;
                font-size: 14px;
                line-height: 25px;
                margin: 20px 0;
                background: #f8f8f8;
                display: flex;
            }

            .source ul {
                padding: 0;
                margin: 0;
                list-style: none;
            }

            .source ul li {
                padding: 0 8px;
            }

            .source ul.line {
                border-right: 1px solid #328cc9;
            }


            .source ul.code {
                flex: 1;
            }


            .source ul li.active {
                background: #ffffff;
                font-weight: bolder;
                color: red;
            }

            .trace {
                list-style: none;
                padding: 15px;
                margin: 0;
                background: #f8f8f8;
                font-size: 14px;
                line-height: 25px;
            }
        </style>
    </head>
    <body>
    <?php if (true): ?>
        <h1><?= $this->message ?></h1>
        <h3><?= "{$this->file} line :{$this->line}" ?></h3>
        <?php
        if ($this->subMessage) {
            echo "<div class='code'>{$this->subMessage}</div>";
        }
        ?>
        <div class="source">
            <ul class="line">
                <?php
                $i = $start + 1;
                foreach ($nl as $l) {
                    if ($i == $this->line) {
                        $pre = 'active';
                    } else {
                        $pre = '';
                    }
                    ?>
                    <li class="<?= $pre ?>"><?= $i ?></li>
                    <?php
                    $i++;
                } ?>
            </ul>
            <ul class="code"><?php
                $i = $start + 1;
                foreach ($nl as $l) {
                    if ($i == $this->line) {
                        $pre = 'active';
                    } else {
                        $pre = '';
                    }
                    ?>
                    <li class="<?= $pre ?>"><?= $l ?></li>
                    <?php
                    $i++;
                } ?></ul>
        </div>
        <ul class="trace">
            <?php
            if ($this->trace) {
                foreach ($this->trace as $item) {
                    echo "<li>";
                    //array(6) { ["file"]=> string(62) "D:\HTTPService\wwwroot\paperphp\paperphp\library\Container.php" ["line"]=> int(196) ["function"]=> string(5) "__get" ["class"]=> string(15) "paper\Container" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(3) "app" } }

                    echo '<b>' . ($item['file'] ?? '') . "</b>&nbsp;:";
                    echo ($item['line'] ?? '') . "&nbsp;";
                    echo ($item['class'] ?? '') . "&nbsp;";
                    echo ($item['type'] ?? '') . "&nbsp;";
                    echo ($item['function'] ?? '') . "&nbsp;";
//            echo "args&nbsp;";
//             var_dump($item['args']);
                    echo "</li>";
                }
            }
            ?>
        </ul>
        <ul class="trace">
            <?php
            if ($_SERVER) {
                foreach ($_SERVER as $key => $item) {
                    echo "<li>";
                    echo "<b>$key</b> => $item";
                    echo "</li>";

                }
            }
            ?>
        </ul>
    <?php else: ?>
        <h1>出错啦</h1>
    <?php endif; ?>

    </body>
        </html><?php
    }
}

